fix: reduce false positives from local variable shadowing and early-return guards#687
Open
Akshay090 wants to merge 2 commits intoamilajack:mainfrom
Open
fix: reduce false positives from local variable shadowing and early-return guards#687Akshay090 wants to merge 2 commits intoamilajack:mainfrom
Akshay090 wants to merge 2 commits intoamilajack:mainfrom
Conversation
…rowser APIs The Identifier visitor that tracks locally declared names was missing ArrowFunctionExpression and FunctionExpression parent types. This caused false positives when callback parameters happened to share a name with a browser API (e.g. `items.map(scheduler => scheduler.name)` flagged as unsupported Window.scheduler usage). Made-with: Cursor
The existing isInsideIfStatement heuristic only suppressed errors for
API usage inside an if block. The common early-return pattern was missed:
if (!('serviceWorker' in navigator)) { return; }
navigator.serviceWorker.register('/sw.js'); // falsely flagged
Now walks up from the flagged node to its containing block and checks
preceding sibling if-statements for early exits (return/throw) whose
test references the same API. Respects ignoreConditionalChecks setting.
Made-with: Cursor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #688
Summary
Two related fixes that reduce false positives in the
compat/compatrule:1. Function parameter names matching browser APIs
The
Identifiervisitor that tracks locally declared names was missingArrowFunctionExpressionandFunctionExpressionparent types. This caused false positives when callback parameters happened to share a name with a browser API.Example — flagged incorrectly before this fix:
The existing heuristic already handled
FunctionDeclarationparams,VariableDeclarator,Property,ClassDeclaration, and import specifiers. This adds the two missing function types for consistency.2. Early-return guard pattern as feature detection
The existing
isInsideIfStatementheuristic only suppressed errors for API usage inside anifblock. The common early-return guard pattern was missed:The new
isGuardedByEarlyReturnhelper walks up from the flagged node to its containing block and checks preceding siblingif-statements for early exits (return/throw) whose test references the same API. Respects theignoreConditionalCheckssetting.Test plan
inoperator, member expression,throw, bare identifier)ignoreConditionalChecksoverrides early-return detection)